﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using HB.Expand;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Diagnostics;

namespace Test
{

    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            a.ss = 100;
            a.ff = 233;
            B bb = new B();
            a.CopyToOther(bb);
            //CRC测试
            byte[] crcd = new byte[] { 0x43, 0x89, 0x80, 0xf9 };
            Console.WriteLine(crcd.ToCRC8MAXIM());
            Console.WriteLine(crcd.ToCRC16MODBUS());
            Console.WriteLine(crcd.ToCRC8MAXIM(1, 2));
            //  Console.WriteLine(crcd.ToCRC16(1,2));

            float f = 123.45634223232f;
            Console.WriteLine(f.ToStringF2());
            Console.ReadLine();
            return;
            //string b1 = "45";
            //string b22 = "45a";
            //var rb1 = b1.TryToInt(50);
            //var rb2 = b22.TryToInt(50);
            //b1.TryToByte(12);
            //var b2 = b1.IsBool();
            //var b38 = bool.TryParse(b1, out b2);
            //System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
            //DateTime time11 = new DateTime(2021, 12, 27);
            //DateTime time12 = new DateTime(2022, 1, 2);
            //var v3 = gc.GetDayOfWeek(time12);

            //string s = "342rwdfwaudbwyfbwifbhiwbfiubhfiubfifkjfnkjsnskjfibeibei";
            //Console.WriteLine(s.LimitLen(10));

            //Console.ReadKey();
            Random R = new Random();
            var m = new model()
            {
                b = R.Next(0, 10) > 5,
                d = R.NextDouble(),
                f = (float)R.NextDouble(),
                s16 = (short)R.Next(-10000, 10000),
                s32 = R.Next(-10000, 10000),
                s64 = R.Next(-100000000, 10000000),
                s8 = (sbyte)R.Next(-120, 120),
                u16 = (ushort)R.Next(-10000, 10000),
                u32 = (uint)R.Next(0, 1000000),
                u64 = (ulong)R.Next(0, 1000000000),
                u8 = (byte)R.Next(0, 100),
                bytes = new byte[] { (byte)R.Next(0, 100), (byte)R.Next(0, 100), (byte)R.Next(0, 100), (byte)R.Next(0, 100) },
                ds = new double[] { R.NextDouble(), R.NextDouble(), R.NextDouble(), R.NextDouble() },
                fs = new float[] { (float)R.NextDouble(), (float)R.NextDouble(), (float)R.NextDouble(), (float)R.NextDouble(), },
                s16s = new short[] { (short)R.Next(-10000, 10000), (short)R.Next(-10000, 10000), (short)R.Next(-10000, 10000), (short)R.Next(-10000, 10000), },
                s32s = new int[] { R.Next(-10000, 10000), R.Next(-10000, 10000), R.Next(-10000, 10000), R.Next(-10000, 10000), },
                s64s = new long[] { R.Next(-100000000, 10000000), R.Next(-100000000, 10000000), R.Next(-100000000, 10000000), R.Next(-100000000, 10000000), },
                sbytes = new sbyte[] { (sbyte)R.Next(-120, 120), (sbyte)R.Next(-120, 120), (sbyte)R.Next(-120, 120), (sbyte)R.Next(-120, 120), },
                u16s = new ushort[] { (ushort)R.Next(-10000, 10000), (ushort)R.Next(-10000, 10000), (ushort)R.Next(-10000, 10000), (ushort)R.Next(-10000, 10000), },
                u32s = new uint[] { (uint)R.Next(0, 1000000), (uint)R.Next(0, 1000000), (uint)R.Next(0, 1000000), (uint)R.Next(0, 1000000), },
                u64s = new ulong[] { (ulong)R.Next(0, 1000000000), (ulong)R.Next(0, 1000000000), (ulong)R.Next(0, 1000000000), (ulong)R.Next(0, 1000000000), }

            };
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                var d1 = m.MapToStream(true);
            }
            sw.Stop();
            Console.WriteLine($"分析为字节流1w次：{sw.ElapsedMilliseconds} ms");
            Console.WriteLine($"avg:{sw.ElapsedMilliseconds / 100000.0} ms");

            Console.WriteLine();

            string j1 = JsonSerializer.Serialize(m);
            Console.WriteLine(j1);
            byte[] data = m.MapToStream(true);
            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            for (int i = 0; i < 100000; i++)
            {
                var m2 = data.MapToClass<model>(true);
                //var d1 = a1.MapToStream(true);
            }
            sw2.Stop();
            Console.WriteLine($"解析字节流1w次：{sw2.ElapsedMilliseconds} ms");
            Console.WriteLine($"avg:{sw2.ElapsedMilliseconds / 100000.0} ms");
            var m3 = data.MapToClass<model>(true);
            Console.WriteLine(JsonSerializer.Serialize(m3.Data));
            //string j2 = JsonSerializer.Serialize(a2.Data);
            //Console.WriteLine(j2);
            //Console.WriteLine(j1 == j2);

            Console.ReadKey();
            //尝试转换指定的日期格式
            string timestr = "2020-02-03";
            bool b3 = timestr.TryToDateTime("yyyy-MM-dd", out DateTime temp);//尝试转换指定的日期格式
            DateTime time = timestr.ToDateTime("yyyy-MM-dd");//直接转为datetime

            //获取时间戳
            long t = DateTime.Now.ToTimeStamp();//转换为时间戳，以秒为单位
            long t1 = DateTime.Now.ToTimeStampMs();// 转换为时间戳，以毫秒为单位
            long t2 = DateTime.Now.ToTimeStampOfDayStart();//当天00:00:00的时间戳
            long t3 = DateTime.Now.ToTimeStampOfMonthStart();//当月1号00:00：00的时间戳

            long second = 1567847658;
            DateTime dt1 = second.ToDateTimeFromSecond();//从时间戳（秒）转为DataTime
            DateTime dt11 = second.ToDateTimeFromMilliSecond();//从时间戳（毫秒）转为DataTime

            int second2 = 1567847658;
            DateTime dt2 = second2.ToDateTimeFromSecond(); //从时间戳（秒）转为DataTime
            DateTime dt21 = second2.ToDateTimeFromMilliSecond();//从时间戳（毫秒）转为DataTime

            double second3 = 15743788457.5654433;
            DateTime dt3 = second3.ToDateTimeFromSecond(); //从时间戳（秒）转为DataTime
            DateTime dt31 = second3.ToDateTimeFromMilliSecond();//从时间戳（毫秒）转为DataTime

            float second4 = 31332123123123.433f;
            DateTime dt4 = second4.ToDateTimeFromSecond(); //从时间戳（秒）转为DataTime
            DateTime dt41 = second4.ToDateTimeFromMilliSecond();//从时间戳（毫秒）转为DataTime

            DateTime now = DateTime.Now;
            now.ToString_yyyyMMdd();//yyyyMMdd
            now.ToString_yyyyMMddHHmmss();//yyyy/MM/dd HH:mm:ss
            now.ToString_yyyyMMddHHmmssffffff();//yyyy/MM/dd HH:mm:ss.ffffff
            now.ToInt_yyyyMMdd(); //yyyyMMdd

            var week = now.WeekOfYear();//本年度第几周
            int w = week.WeekIndex;//1-52

            var quarter = now.QuarterOfYear();//本年度第几季度，1-4

            var range1 = now.GetRangeOfBelongWeek();//获取所属周开始和结束日期
            var s11 = range1.Start;
            var s12 = range1.End;

            var range2 = now.GetRangeOfBelongMonth();//获取所属月开始和结束日期
            var s21 = range2.Start;
            var s22 = range2.End;

            var range3 = now.GetRangeOfBelongQuarter();//获取所属季度开始和结束日期
            var s31 = range3.Start;
            var s32 = range3.End;

            var range4 = now.GetRangeOfBelongYear();//获取所属年开始和结束日期
            var s41 = range4.Start;
            var s42 = range4.End;

            Dictionary<string, int> dic = new Dictionary<string, int>();

            Console.ReadKey();
        }
    }
    struct A
    {
        public int ss { get; set; }
        public double ff { get; set; }
    }
    class B
    {
        public int ss { get; set; }
        public double ff { get; set; }
    }
    class model
    {
        public bool b { get; set; }
        public sbyte s8 { get; set; }

        public sbyte[] sbytes { get; set; } = new sbyte[4];
        public byte u8 { get; set; }

        public byte[] bytes { get; set; } = new byte[4];
        public ushort u16 { get; set; }

        public ushort[] u16s { get; set; } = new ushort[4];
        public short s16 { get; set; }

        public short[] s16s { get; set; } = new short[4];
        public uint u32 { get; set; }

        public uint[] u32s { get; set; } = new uint[4];
        public int s32 { get; set; }

        public int[] s32s { get; set; } = new int[4];
        public ulong u64 { get; set; }

        public ulong[] u64s { get; set; } = new ulong[4];
        public long s64 { get; set; }

        public long[] s64s { get; set; } = new long[4];
        public float f { get; set; }

        public float[] fs { get; set; } = new float[4];
        public double d { get; set; }

        public double[] ds { get; set; } = new double[4];
    }
}
